<template>
    <div id="calendar">
        <div class="calendar-panel">
            <!-- 年份 月份 -->
            <div class="month">
                <!--<ul>-->
                <!--&lt;!&ndash;点击会触发pickpre函数，重新刷新当前日期 @click(vue v-on:click缩写) &ndash;&gt;-->
                <!--<li class="arrow" @click="pickPre(currentYear,currentMonth)">❮</li>-->
                <!--<li class="year-month" @click="pickYear(currentYear,currentMonth)">-->
                <!--<span class="choose-year">{{ currentYear }}</span>-->
                <!--<span class="choose-month">{{ currentMonth }}月</span>-->
                <!--</li>-->
                <!--<li class="arrow" @click="pickNext(currentYear,currentMonth)">❯</li>-->
                <!--</ul>-->
            </div>
            <!-- 星期 -->
            <ul class="weekdays">
                <li>一</li>
                <li>二</li>
                <li>三</li>
                <li>四</li>
                <li>五</li>
                <li style="color:red">六</li>
                <li style="color:red">日</li>
            </ul>
            <!-- 日期 -->
            <ul class="days">
                <template v-for="day in dayList">
                    <li :class="{'other-month' : !day.current,'current-date':day.current && day.day === currentDate.day}" @click="clickDay(day)">
                        <span v-if="day.isRest" class="day-rest">休</span>
                        <span v-if="day.isShift" class="day-shift">班</span>
                        <span>{{day.day}}</span>
                        <span class="days-lunar">{{day.lunar.IDayCn}}</span>
                    </li>
                </template>

            </ul>
        </div>
        <div class="day-info">
            <span>{{checkDay.day}}</span>
            <span>{{checkDay.year}} / {{checkDay.month}}</span>
            <!--{{checkDay}}-->
        </div>
    </div>
</template>

<script>

    import {baseUtil} from '@/util/BaseUtil';
    import {calendar} from '@/util/LunarCalendarUtil';

    export default {
        name: "el-calendar",
        props: {
            calendarDate : {
                type: String,
                default: new Date()
            }
        },
        data() {
            return {
                baseUtil : baseUtil,
                calendar : calendar,
                currentDate : baseUtil.formatDate(this.calendarDate, null, null, true),
                weekdays : [],
                dayList : [],
                checkDay : {},
            }
        },
        methods: {
            // 初始化日期
            initDays(date) {
                let dayNum = this.getDayNum(date.year, date.month);
                let firstDay = this.baseUtil.formatDate(date.year + "-" + date.month + "-01", null, null, true);
                // console.log(firstDay);
                // 计算开始日期
                let dayList = [];
                // 补充前期
                // 获取上月的天数
                if(firstDay.week > 1 || firstDay.week === 0) {
                    let tempNum = firstDay.week - 1;
                    if(firstDay.week === 0) {
                        tempNum = 6;
                    }
                    let year = date.month === 1 ? date.year - 1 : date.year;
                    let month = date.month === 1 ? 12 : date.month - 1;
                    let lastDayNum = this.getDayNum(year, month);
                    for(let i = tempNum; i > 0; i--) {
                        dayList.push({
                            year: year,
                            month: month,
                            week : i,
                            day: lastDayNum - i + 1,
                            current : false,
                            lunar : this.calendar.solar2lunar(year, month, lastDayNum - i),
                            isRest : false,
                            isShift : false,
                        });
                    }
                }
                let flag = this.currentDate.year === date.year && this.currentDate.month === date.month && this.currentDate.day === date.day;
                for(let i = 1; i <= dayNum; i++) {
                    let day = {
                        year: date.year,
                        month: date.month,
                        week : date.week,
                        day: i,
                        // 当月
                        current : true,
                        lunar : this.calendar.solar2lunar(date.year, date.month, i),
                        isRest : false,
                        isShift : false,
                    };
                    if(flag) {
                        this.checkDay = day;
                    }
                    dayList.push(day);
                }
                // 补充后期
                let otherNum = 42 - dayList.length;
                if(otherNum > 0) {
                    // 获取下月信息
                    let year = date.month === 12 ? date.year + 1 : date.year;
                    let month = date.month === 12 ? 1 : date.month + 1;
                    for(let i = 1; i <= otherNum; i++) {
                        dayList.push({
                            year: year,
                            month: month,
                            week : i,
                            day: i,
                            current : false,
                            lunar : this.calendar.solar2lunar(year, month, i),
                            isRest : false,
                            isShift : false,
                        });
                    }
                }
                // console.log(dayList);
                this.dayList = dayList;
            },
            // 获取当月有多少天
            getDayNum(year, month) {
                let num = 0;
                switch (month) {
                    case 1:case 3:case 5:case 7:case 8:case 10:case 12:
                    num = 31;
                    break;
                    case 4:case 6:case 9:case 11:
                    num = 30;
                    break;
                    case 2:
                        num = (year % 4 === 0) && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28;
                        break;
                }
                return num;
            },
            // 点击日期
            clickDay(day) {
                this.checkDay = day;
                console.log(day);
            }
        },
        created() {
            this.initDays(this.currentDate);
        }
    }
</script>

<style lang="scss" scoped>

    * {
        box-sizing: border-box;
    }

    #calendar{

        .calendar-panel{
            animation: showPanel 5s;
            /*opacity : 0;*/
            width:70%;
            display: inline-block;
            float: left;
            /*margin: 0 auto;*/
            box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.1), 0 1px 5px 0 rgba(0,0,0,0.12);

            ul.weekdays{
                margin: 0;
                padding: 10px 0;
                background-color: #00B8EC;
                display: flex;
                flex-wrap: wrap;
                color: #FFFFFF;
                justify-content: space-around;

                li{
                    display: inline-block;
                    width: 13.6%;
                    text-align: center;
                }
            }

            ul.days{
                padding: 0;
                background: #FFFFFF;
                margin: 0;
                display: flex;
                flex-wrap: wrap;
                justify-content: space-around;

                li{
                    position: relative;
                    list-style-type: none;
                    display: inline-block;
                    width: 14.2%;
                    text-align: center;
                    padding-bottom: 15px;
                    padding-top: 15px;
                    font-size: 1rem;
                    color: #000;
                    cursor: pointer;
                    overflow: hidden;

                    transition: background-color 0.4s;

                    &.other-month {
                        /*padding: 5px;*/
                        color: gainsboro;
                    }

                    &.current-date{
                        color: red;
                    }

                    .days-lunar{
                        display: block;
                        font-size: 8px;
                    }

                    .day-rest,.day-shift{
                        display: block;
                        position: absolute;
                        padding: 0;
                        margin: 0;
                        top: 3px;
                        right: -38px;
                        width: 100px;
                        height: 18px;
                        background-color: #ce2d23;
                        transform:rotate(45deg);
                        -ms-transform:rotate(45deg); 	/* IE 9 */
                        -moz-transform:rotate(45deg); 	/* Firefox */
                        -webkit-transform:rotate(45deg); /* Safari 和 Chrome */
                        -o-transform:rotate(45deg); 	/* Opera */
                        color: white;
                        font-family: 黑体,serif;
                        line-height: 20px;
                        font-size: 14px;
                    }

                    .day-shift{
                        background-color: #becdce;
                    }

                    &:hover{
                        background-color: #e1e1e1;
                    }

                }
            }
        }

        .day-info{
            float: left;
            display: inline-block;
            background-color: white;
            height: 300px;
            width: 30%;
        }

    }

    @keyframes showPanel {
        from {opacity:0.0;}
        to {opacity:1;}
    }


</style>
